package cn.ronghuanet.org.controller;

import cn.ronghuanet.base.util.PageList;
import cn.ronghuanet.org.domain.Employee;
import cn.ronghuanet.org.domain.Employee;
import cn.ronghuanet.org.query.EmployeeQuery;
import cn.ronghuanet.org.service.IEmployeeService;
import cn.ronghuanet.base.util.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController //当前类默认返回json格式
@RequestMapping("/employee")
@Api(tags = "员工管理-蓉易学")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    //1.查询所有
    //localhost:8080/employee   get请求方式
    @GetMapping //get请求
    @ApiOperation(value = "查询所有")
    public AjaxResult loadAll(){
        //返回值,默认就是成功
        AjaxResult ajaxResult=AjaxResult.me();
        try {
            List<Employee> list = employeeService.loadAll();
            ajaxResult.setResultObj(list);
        } catch (Exception e) {
            e.printStackTrace();
            //返回失败 {"success":false,"message":.....}
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("网络异常,请稍后重试!");
        }
        return ajaxResult;
    }
    //2.查询单个
    //localhost:8080/employee/1   get请求方式
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个")
    public AjaxResult getOne(@PathVariable("id") Long id){
        //返回值,默认就是成功
        AjaxResult ajaxResult=AjaxResult.me();
        try {
            Employee one = employeeService.getOne(id);
            ajaxResult.setResultObj(one);
        } catch (Exception e) {
            e.printStackTrace();
            //返回失败 {"success":false,"message":.....}
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("网络异常,请稍后重试!");
        }
        return ajaxResult;
    }
    //3.新增or修改
    //localhost:8080/employee   put请求方式
    @PutMapping
    @ApiOperation(value = "新增or修改")
    public AjaxResult saveEmployee(@RequestBody Employee employee){
        //返回值,默认就是成功
        AjaxResult ajaxResult=AjaxResult.me();
        try {
            //新增没有id,修改有id
            Long id = employee.getId();
            if (id==null){
                employeeService.insertOne(employee);
            }else {
                employeeService.updateOne(employee);
            }
        } catch (Exception e) {
            e.printStackTrace();
            //返回失败 {"success":false,"message":.....}
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("网络异常,请稍后重试!");
        }
        return ajaxResult;
    }
    //4.删除单个
    //localhost:8080/employee/1  delete请求方式
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除单个")
    public AjaxResult deleteEmployeeById(@PathVariable("id") Long id){
        //返回值,默认就是成功
        AjaxResult ajaxResult=AjaxResult.me();
        try {
            employeeService.deleteOne(id);
        } catch (Exception e) {
            e.printStackTrace();
            //返回失败 {"success":false,"message":.....}
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("网络异常,请稍后重试!");
        }
        return ajaxResult;
    }
    //5.批量删除
    //localhost:8080/employee   Patch请求方式
    @PatchMapping
    @ApiOperation(value = "批量删除")
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        //返回值,默认就是成功
        AjaxResult ajaxResult=AjaxResult.me();
        try {
            employeeService.patchDel(ids);
        } catch (Exception e) {
            e.printStackTrace();
            //返回失败 {"success":false,"message":.....}
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("网络异常,请稍后重试!");
        }
        return ajaxResult;
    }


    //6.分页查询
    @PostMapping
    @ApiOperation(value = "分页查询")
    public AjaxResult queryPage(@RequestBody EmployeeQuery employeeQuery){
        //返回值,默认就是成功
        AjaxResult ajaxResult=AjaxResult.me();
        try {
            PageList<Employee> list = employeeService.queryPage(employeeQuery);
            ajaxResult.setResultObj(list);
        } catch (Exception e) {
            e.printStackTrace();
            //返回失败 {"success":false,"message":.....}
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("网络异常,请稍后重试!");
        }
        return ajaxResult;
    }
}
